package tv.youxing.android.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;

import com.kymjs.rxvolley.RxVolley;
import com.kymjs.rxvolley.client.HttpCallback;
import com.kymjs.rxvolley.client.HttpParams;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import de.hdodenhof.circleimageview.CircleImageView;
import tv.youxing.android.R;
import tv.youxing.android.adapter.AlbumRecommendAdapter;
import tv.youxing.android.adapter.TagAdapter;
import tv.youxing.android.custom.CustomGridView;
import tv.youxing.android.custom.CustomListView;
import tv.youxing.android.custom.CustomToast;
import tv.youxing.android.entity.AlbumInfoModel;
import tv.youxing.android.entity.DianZhanModel;
import tv.youxing.android.ui.LoginActivity;
import tv.youxing.android.ui.ZhuanJiFenLeiActivity;
import tv.youxing.android.utils.AppConst;
import tv.youxing.android.utils.GsonUtils;
import tv.youxing.android.utils.PicassoUtils;
import tv.youxing.android.utils.ShareUtils;
import tv.youxing.android.utils.StaticClass;

/**
 * Created by apple on 2017/4/22.
 */

/**
 * 项目名：   youxing
 * 包名：     tv.youxing.android .fragment
 * 文件名：   DynamicDetailFragment
 * 创建者：   叶克武
 * 创建时间： 2017/4/22 上午9:52
 * 描述：    TODO
 */
public class DynamicDetailFragment extends Fragment {

    @BindView(R.id.intro)
    TextView intro;
    @BindView(R.id.iv_user_head)
    CircleImageView ivUserHead;
    @BindView(R.id.user_name)
    TextView userName;
    @BindView(R.id.user_guanzhu_count)
    TextView userGuanzhuCount;
    @BindView(R.id.add_guanzhu)
    ImageView addGuanzhu;
    @BindView(R.id.user_intro)
    TextView userIntro;
    @BindView(R.id.tagGridView)
    CustomGridView tagGridView;
    @BindView(R.id.lv_album_recommend)
    CustomListView listView;
    Unbinder unbinder;
    private List<String> tagList = new ArrayList<>();

    AlbumInfoModel allData;
    List<AlbumInfoModel.ResultBean.AlbumTagsBean> mAlbumTagsBeanList = new ArrayList<>();
    private List<AlbumInfoModel.ResultBean.RecommedAlbumsBean> albumRecommendModelList = new ArrayList<>();

    public DynamicDetailFragment(AlbumInfoModel allData) {
        this.allData = allData;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_dynamic_detail, null);
        unbinder = ButterKnife.bind(this, view);
        findView();
        return view;
    }

    private void findView() {
        mAlbumTagsBeanList = allData.getResult().getAlbum_tags();
        albumRecommendModelList = allData.getResult().getRecommed_albums();

        if (allData.getResult().getTeacher_info().getAvatar() != null || allData.getResult().getTeacher_info().getAvatar()!="") {
        }

        PicassoUtils.loadImageViewHolder(getContext(),allData.getResult().getTeacher_info().getAvatar(),R.drawable.me_def_avater,R.drawable.me_def_avater,ivUserHead);
        intro.setText("     "+ allData.getResult().getIntro().toString());
        userName.setText(allData.getResult().getTeacher_info().getName());
        userIntro.setText("     "+allData.getResult().getTeacher_info().getIntro());
        userGuanzhuCount.setText("已被 "+allData.getResult().getTeacher_info().getFollow_count() +" 人关注");
        //关注

        if (allData.getResult().getTeacher_info().getIs_follow() == 1) {
            addGuanzhu.setSelected(true);
        } else if (allData.getResult().getTeacher_info().getIs_follow() == 0) {
            addGuanzhu.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
//                //post请求简洁版实现

                    if (allData.getResult().getTeacher_user_id()==0 || allData.getResult().getTeacher_info().getUser_id()==0) {
                        CustomToast.INSTANCE.showToast("非平台用户不能关注 ");
                        return;
                    }

                    HttpParams params = new HttpParams();
                    params.putHeaders("Authorization","Bearer "+ ShareUtils.getString(getContext(), AppConst.TOKEN,""));
                    params.put("follow_user_id",allData.getResult().getTeacher_user_id());

                    RxVolley.post(StaticClass.GuanZhu, params, new HttpCallback() {
                        @Override
                        public void onSuccess(String t) {
                            DianZhanModel model = GsonUtils.GsonToBean(t,DianZhanModel.class);

                            if (model.getCode() == AppConst.TOKENERROR) {
                                getActivity().startActivity(new Intent(getContext(),LoginActivity.class));
                                return;
                            }

                            CustomToast.INSTANCE.showToast(model.getStatus());
                            if (model.getCode() == 200) {
                                addGuanzhu.setSelected(true);
                                addGuanzhu.setEnabled(false);
                            }
                        }
                    });
                }
            });
        }





        tagGridView.setAdapter(new TagAdapter(getActivity(), mAlbumTagsBeanList));
        tagGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                System.out.println(mAlbumTagsBeanList.get(position).getTag_name());
            }
        });

        listView.setAdapter(new AlbumRecommendAdapter(getActivity(), albumRecommendModelList));
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(getActivity(), ZhuanJiFenLeiActivity.class);
                intent.putExtra("album_classify_id",albumRecommendModelList.get(position).getAlbum_classify_id()+"");

                startActivity(intent);
            }
        });

    }



    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
